package com.morven.workpiece.web.controller.craft;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.morven.workpiece.common.annotation.Log;
import com.morven.workpiece.common.core.controller.BaseController;
import com.morven.workpiece.common.core.domain.AjaxResult;
import com.morven.workpiece.common.core.domain.entity.SysMenu;
import com.morven.workpiece.common.core.page.TableDataInfo;
import com.morven.workpiece.common.enums.BusinessType;
import com.morven.workpiece.common.utils.StringUtils;
import com.morven.workpiece.common.utils.poi.ExcelUtil;
import com.morven.workpiece.craft.domain.CraftProceCategoryType;
import com.morven.workpiece.craft.service.ICraftProceCategoryTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 工序类别Controller
 *
 * @author Morven
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/craft/category")
public class CraftProceCategoryTypeController extends BaseController {
    @Autowired
    private ICraftProceCategoryTypeService craftProceCategoryTypeService;

    /**
     * 查询工序类别列表
     */
    @PreAuthorize("@ss.hasPermi('craft:category:list')")
    @GetMapping("/list")
    public AjaxResult list(CraftProceCategoryType craftProceCategoryType) {
        List<CraftProceCategoryType> list = craftProceCategoryTypeService.selectCraftProceCategoryTypeList(craftProceCategoryType);
        return AjaxResult.success(list);
    }

    /**
     * 导出工序类别列表
     */
    @PreAuthorize("@ss.hasPermi('craft:category:export')")
    @Log(title = "工序类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CraftProceCategoryType craftProceCategoryType) {
        List<CraftProceCategoryType> list = craftProceCategoryTypeService.selectCraftProceCategoryTypeList(craftProceCategoryType);
        ExcelUtil<CraftProceCategoryType> util = new ExcelUtil<CraftProceCategoryType>(CraftProceCategoryType.class);
        util.exportExcel(response, list, "工序类别数据");
    }

    /**
     * 获取工序类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('craft:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(craftProceCategoryTypeService.selectCraftProceCategoryTypeById(id));
    }

    /**
     * 获取工序父类下拉列表
     */
    @GetMapping("/listParentCategory")
    public AjaxResult listParentCategory() {
        CraftProceCategoryType categoryType = new CraftProceCategoryType();
        categoryType.setParentId(0L);
        return AjaxResult.success(craftProceCategoryTypeService.selectCraftProceCategoryTypeList(categoryType));
    }

    /**
     * 新增工序类别
     */
    @PreAuthorize("@ss.hasPermi('craft:category:add')")
    @Log(title = "工序类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CraftProceCategoryType craftProceCategoryType) {
        craftProceCategoryType.setCreateUserId(getUserId());
        craftProceCategoryType.setOpUserId(getUserId());
        return toAjax(craftProceCategoryTypeService.insertCraftProceCategoryType(craftProceCategoryType));
    }

    /**
     * 修改工序类别
     */
    @PreAuthorize("@ss.hasPermi('craft:category:edit')")
    @Log(title = "工序类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CraftProceCategoryType craftProceCategoryType) {
        return toAjax(craftProceCategoryTypeService.updateCraftProceCategoryType(craftProceCategoryType));
    }

    /**
     * 删除工序类别
     */
    @PreAuthorize("@ss.hasPermi('craft:category:remove')")
    @Log(title = "工序类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        String result = craftProceCategoryTypeService.checkCategory(ids);
        if (StringUtils.isEmpty(result)) {
            return toAjax(craftProceCategoryTypeService.deleteCraftProceCategoryTypeByIds(ids));
        } else {
            return AjaxResult.error("工序类别'" + result + "'已关联数据，请重新选择！");
        }
    }

}
